package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ljye.jg.ljye.App;
import com.ljye.jg.ljye.R;
import com.ljye.jg.ljye.utils.HttpUtils;

import java.io.IOException;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//成果案例
public class Activity_Successful_Case extends Activity {
    private ImageView back;//返回
    private String token;
    private TextView baocun;
    private EditText biaoti;
    private EditText context;
    private OkHttpClient client;
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.baocun://保存
                    up_date();
                    break;

                default:
                    break;
            }
        }
    };
    private Gson gson;

    private void up_date() {
        String bt = biaoti.getText().toString();
        final String text = context.getText().toString();
        new Thread() {
            @Override
            public void run() {
                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("content", text != null ? text : " ")
                        .build();

                Request request = new Request.Builder()
                        .url(HttpUtils.PATH + "/case")
                        .addHeader("Accept", HttpUtils.MYAPP)
                        .addHeader("Authorization", "Bearer" + " " + token)
                        .post(body)
                        .build();

                try {
                    Response execute = client.newCall(request).execute();
                    String up_string = execute.body().string();
                    Log.d("Activity_up", up_string);
                    if (execute.isSuccessful()) {


                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__successful__case);
        initView();
        App application = (App) getApplication();
        token = application.getToken();
        client = App.getClient();
        back.setOnClickListener(listener);
        baocun.setOnClickListener(listener);
        getdata();


    }

    public void getdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder()
                            .url(HttpUtils.PATH + "/case")
                            .addHeader("Accept", HttpUtils.MYAPP)
                            .addHeader("Authorization", "Bearer" + " " + token)
                            .build();
                    Response response = client.newCall(request).execute();

                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Log.d("Activity_AA", string);

                        gson = new Gson();
//                        Teacher_base teacher_base = gson.fromJson(string, Teacher_base.class);
//                        teacher = teacher_base.getData().getTeacher();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                setdata();
                            }
                        });

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setdata() {

    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        baocun = (TextView) findViewById(R.id.baocun);
        biaoti = (EditText) findViewById(R.id.biaoti);
        context = (EditText) findViewById(R.id.context);
    }

    //收起软键盘
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
